//
//  YHBaseTableViewCell.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/9/17.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "YHBaseTableViewCell.h"
#import <Masonry/Masonry.h>
#import <YHStyle/UIFont+YH.h>
#import <YHStyle/UIColor+YHStyle.h>

@interface YHBaseTableViewCell ()

//@property (retain, nonatomic) CAShapeLayer *shapeLayer;

@property (retain, nonatomic, readwrite) UIImageView * accessoryArrowView;

@property (retain, nonatomic) UIView * lineVBottom;
@property (retain, nonatomic) UIView * lineVTop;


@property (retain, nonatomic) UIColor * originColorBackgroud;
@property (retain, nonatomic) UIColor * originColorContent;
@property (retain, nonatomic) UIColor * originColorContentCustom;

@end

@implementation YHBaseTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [self yh_nibConfig];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self){
        [self defaultStyle];
        [self yh_commonInit];
    }
    return self;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if([reuseIdentifier isEqualToString:self.class.cellIndentifyValue1]){
        style = UITableViewCellStyleValue1;
    }
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self defaultStyle];
        [self yh_commonInit];
        [self yh_commonInitWithReuseIdentifier:reuseIdentifier];
    }
    return self;
}

- (void)defaultStyle{
    self.textLabel.textColor = [UIColor yh_h1];
    self.textLabel.font = [UIFont yh_systemFontOfSize:16];
    self.selectedBackgroundView = [UIView new];
    self.multipleSelectionBackgroundView = [UIView new];
    
    self.contentView.backgroundColor = [UIColor yh_background];
    self.backgroundColor = [UIColor yh_background];
    
    self.tintColor = [UIColor yh_blue];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
    // Configure the view for the selected state
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    if(!self.highlighted && !highlighted){
        [super setHighlighted:highlighted animated:animated];
        return;
    }
    BOOL willChange = (self.highlighted != highlighted);
    if(!self.originColorBackgroud){
        self.originColorBackgroud = self.backgroundColor;
    }
    if(!self.originColorContent){
        self.originColorContent = self.contentView.backgroundColor;
    }
    if(!self.originColorContentCustom){
        self.originColorContentCustom = self.cellContent.backgroundColor;
    }
    
    [super setHighlighted:highlighted animated:animated];
    
    if(!willChange){
        return;
    }
    
    if(self.originColorBackgroud &&
       ![self.originColorBackgroud yh_isEqualToColor:[UIColor clearColor]]){
        if(highlighted){
            self.backgroundColor = [self.originColorBackgroud darkerColor:0.9];
        }else{
            self.backgroundColor = self.originColorBackgroud;
        }
    }
    if(self.originColorContent &&
       ![self.originColorContent yh_isEqualToColor:[UIColor clearColor]]){
        if(highlighted){
            self.contentView.backgroundColor = [self.originColorContent darkerColor:0.9];
        }else{
            self.contentView.backgroundColor = self.originColorContent;
        }
    }
    if(self.originColorContentCustom &&
       ![self.originColorContentCustom yh_isEqualToColor:[UIColor clearColor]]){
        if(highlighted){
            self.cellContent.backgroundColor = [self.originColorContentCustom darkerColor:0.9];
        }else{
            self.cellContent.backgroundColor = self.originColorContentCustom;
        }
    }
}




#pragma mark -- 子类重写方法
- (void)yh_commonInit{}
- (void)yh_commonInitWithReuseIdentifier:(NSString *)reuseIdentifier{
}
- (void)configCellWithData:(id)data{}

- (void)yh_nibConfig{}

+ (CGFloat)cellHeightWithData:(id)data
{
    return 44.f;
}

+ (NSString *)cellIndentify{
    return [NSString stringWithFormat:@"tableViewReuseIndentify_%@",NSStringFromClass(self)];
}

+ (NSString *)cellIndentifyValue1{
    return [NSString stringWithFormat:@"tableViewReuseIndentify_%@_value1",NSStringFromClass(self)];
}

/// 有左右边距的类型
+ (NSString *)cellIndentifyMarginSpace{
    return [NSString stringWithFormat:@"cellIndentifyMarginSpace_%@",NSStringFromClass(self)];
}





//- (void)renderAtTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath leftSpace:(CGFloat)space {
//
//    if (!tableView) {
//        return;
//    }
//
//    CGFloat cornerRadius = 4.0;
//    CGRect bounds = self.contentView.bounds;
//    bounds.size.width = APP.info.s_width - 32;
//    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
//    BOOL addLine = NO;
//    UIBezierPath *bezierPath = nil;
//
//    if (indexPath.row == 0 && numberOfRows == 1) {
//        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
//    } else if (indexPath.row == 0) {
//        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
//        addLine = YES;
//    } else if (indexPath.row == numberOfRows - 1) {
//        bezierPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
//    } else {
//        bezierPath = [UIBezierPath bezierPathWithRect:bounds];
//        addLine = YES;
//    }
//    self.backgroundColor = [UIColor clearColor];
//
//    if(self.shapeLayer){
//        [self.shapeLayer removeFromSuperlayer];
//        self.shapeLayer = nil;
//    }
//
//    self.shapeLayer = [CAShapeLayer layer];
//    self.shapeLayer.fillColor = [UIColor yh_h1].CGColor;
//    self.shapeLayer.strokeColor = [UIColor clearColor].CGColor;
//    self.shapeLayer.path = bezierPath.CGPath;
//
//    if (addLine) {
//        CALayer *lineLayer = [[CALayer alloc] init];
//        CGFloat lineHeight = 1.0;
//        lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+space, bounds.size.height-lineHeight, bounds.size.width-space *2, lineHeight);
//        lineLayer.backgroundColor = [UIColor yh_colorWithHexString:@"0xEEEEEE"].CGColor;
//        [self.shapeLayer addSublayer:lineLayer];
//    }
//    [self.contentView.layer insertSublayer:self.shapeLayer atIndex:0];
//}

#pragma mark - view

-(UIView *)cellContent{
    if(_cellContent){
        return _cellContent;
    }
    _cellContent = [UIView new];
    _cellContent.backgroundColor = [UIColor yh_background];
    [self.contentView addSubview:_cellContent];
    [_cellContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        if([self.reuseIdentifier containsString:[self.class cellIndentifyMarginSpace]]){
            make.left.equalTo(self.contentView).offset(10);
            make.right.equalTo(self.contentView).offset(-10);
        }else{
            make.left.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
        }
    }];
    return _cellContent;
}

-(UIImageView *)accessoryArrowView{
    if(_accessoryArrowView){
        return _accessoryArrowView;
    }
    _accessoryArrowView = [UIImageView new];
    _accessoryArrowView.image = [UIImage imageNamed:@"arrowRight"];
    _accessoryArrowView.contentMode = UIViewContentModeScaleAspectFit;
    _accessoryArrowView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_accessoryArrowView];
    [_accessoryArrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-10);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    _accessoryArrowView.hidden = YES;
    return _accessoryArrowView;
}

-(void)setShowAccessoryArrowView:(BOOL)showAccessoryArrowView{
    if(_showAccessoryArrowView == showAccessoryArrowView){
        return;
    }
    _showAccessoryArrowView = showAccessoryArrowView;
    self.accessoryArrowView.hidden = !showAccessoryArrowView;
    
    [self reLayeroutContentView];
}


-(void)setShowBottomLine:(BOOL)showBottomLine{
    _showBottomLine = showBottomLine;
    if(!self.lineVBottom){
        self.lineVBottom = [UIView new];
        self.lineVBottom.backgroundColor = [UIColor yh_separator];
        if(self.cellContent){
            [self.cellContent addSubview:self.lineVBottom];
            [self.lineVBottom mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self.cellContent);
                make.height.mas_equalTo(0.5);
            }];
        }else{
            [self.contentView addSubview:self.lineVBottom];
            [self.lineVBottom mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self.contentView);
                make.height.mas_equalTo(0.5);
            }];
        }
    }
    self.lineVBottom.hidden = !showBottomLine;
}

- (void)reLayeroutContentView{
    
}

+ (CGFloat)cellContentHeight{
    return 0;
}

@end


